package com.tul.exercise.base.jmockit.atInjectable;

import mockit.Injectable;
import com.tul.exercise.base.jmockit.atMocked.ToBeMocked;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * 测试 @Mocked 用法
 *
 * @Injectable作用于测试类成员
 * 被该注解修饰的当前类实例被mock
 * 被mocked的类所有非private方法都将处于mocked状态,被mocked的方法默认返回值null
 *
 */
public class MockedAndInjectableTest1 {
    @Injectable
    ToBeMocked instance;

    @Test
    public void test() {
        // 方法fun 被mock，默认返回值为null
        System.out.println(instance.fun());
        assertTrue(instance.fun() == null);

        // 不管methodB方法中调用了私有方法methodA，methodB同样被mock，默认返回值null
        System.out.println(instance.fun());
        assertTrue(instance.methodB() == null);

        // @Injectable 注解只对该类的当前实例进行mock
        ToBeMocked newInstance = new ToBeMocked();
        System.out.println(newInstance.fun());
        assertTrue(newInstance.fun() != null);

    }
}
